#include "../include/head.h"
// 接受用户输入
goodsNode userInput()
{
    printf("请输入商品信息(名称 价格 货架位置 数量)\n");
    goodsNode value;
    scanf("%s %f %s %d", value.name, &value.price, value.location, &value.num);
    return value;
}

// 整个将链表的数据写入到文件
int write_data(LinkList L)
{
    // FILE *fp = fopen(STUDENTS_FILE, "ab+");
    FILE *fp = fopen(STUDENTS_FILE, "w+");
    LinkList p = L->next;
    printf("名称 价格 货架位置 数量\n");
    while (p)
    {
        fprintf(fp, "%s %f %s %d\n", p->data.name, p->data.price, p->data.location, p->data.num);
        p = p->next;
    }
    if (fp == NULL)
    {
        printf("打开文件失败\n");
        return -1;
    }
    printf("写入数据成功\n");
    // fwrite(&value, sizeof(goodsNode), 1, fp);

    fclose(fp);
    return 0;
}

// 将文件的内容读出来存到链表
goodsNode read_data(LinkList L)
{
    goodsNode goods;
    FILE *fp = fopen(STUDENTS_FILE, "r+");
    if (fp == NULL)
    {
        printf("打开文件失败\n");
    }
    char line[256]; // 假设每行数据不会超过255个字符
    while (fgets(line, sizeof(line), fp))
    {
        line[strcspn(line, "\n")] = 0;
        // 解析整行数据
        sscanf(line, "%s %f %s %d", goods.name, &goods.price, goods.location, &goods.num);
        list_tail_insert(L, goods);
    }
    return goods;
}

// 打印单个商品信息
void show_one_goods(goodsNode goods)
{
    print_border();
    printf("+\t名称\t 价格\t\t 货架位置\t 数量\t+\n");
    printf("+\t%s\t%f\t%s\t\t%d\t+\n", goods.name, goods.price, goods.location, goods.num);
}

// 首页提示#include <stdio.h>

#define BORDER_WIDTH 97

void print_border()
{
    for (size_t i = 0; i < BORDER_WIDTH; i++)
    {
        printf("=");
    }
    printf("\n");
}

void print_empty_line()
{
    printf("+\t\t\t\t\t\t\t\t\t\t\t\t+\n");
}

void home_hint()
{
    print_border();
    for (int i = 0; i < 3; ++i)
    {
        print_empty_line();
    }
    printf("+\t\t\t\t\t欢迎使用超市管理系统\t\t\t\t\t+\n");
    print_empty_line();
    printf("+\t\t\t\t输入1录入商品\t\t\t\t\t\t\t+\n");
    printf("+\t\t\t\t输入2查看商品列表\t\t\t\t\t\t+\n");
    printf("+\t\t\t\t输入3选购商品\t\t\t\t\t\t\t+\n");
    printf("+\t\t\t\t输入商品名称查找指定商品信息\t\t\t\t\t+\n");
    for (int i = 0; i < 2; ++i)
    {
        print_empty_line();
    }
    print_border();
}